# -*- coding: utf-8 -*-
# -*- frozen_string_literal: true -*-

require_relative "./../../_config/minitest"
require_relative "./../../../dev/tasks/prep_release"

class TestPrepRelease < Minitest::Test
  # Don't parallelize_me!

  def test_can_build_gem_without_warnings
    skip "Integration: Building gems takes too long" unless ENV["<%= @scaffold.gem_constant %>_ALL_TESTS"]
    _, err = capture_subprocess_io { PrepRelease.package }
    refute_match %r/warning/i, err
  end

def test_generates_checksum_for_packed_gem
expected_checksum =
"bb95b01232696ce38a4ca482aebc5b7037744621eddb37071e543ad55c38db0c"\
"ba0317f089916576b52f3eaddfac0dd3495057ce0fc415032bab04ef3539d2ee"

Dir.mktmpdir do |_|
gem = Tempfile.new.tap{ |g| g.write "ah" }.tap(&:close)

checksum = PrepRelease.checksum gem, archive: _
assert_equal expected_checksum, checksum
end
end

def test_writes_checksum_to_given_directory
Dir.mktmpdir do |dir|
tempfile = Tempfile.new
basename = File.basename tempfile

PrepRelease.checksum tempfile, archive: dir
assert File.exist?("#{dir}/#{basename}.sha512"), "Missing checksum"
end
end

def test_fails_to_commit_checksum_when_checksum_missing
Dir.mktmpdir do |dir|
e = assert_raises { PrepRelease.commit_checksum archive: dir }
assert_equal "Must create checksum file before trying to do commit",
e.message
end
end

def test_fails_to_tag_release_commit_when_checksum_missing
Dir.mktmpdir do |dir|
e = assert_raises { PrepRelease.tag_release_commit archive: dir }
assert_equal "Must add checksum before tagging a release", e.message
end
end

def test_publishes_gem
assert_respond_to PrepRelease, :publish_gem
end
end
